/*
SPI2.c
*/

#include "SPI2_ADC.h"
#include "p24HJ256GP206.h"
#include "main.h"


void SPI2_Delay(long int cycles)
{
float temp;
temp=(float)(cycles*(FREQ/2000000));cycles=(long int)temp;while(cycles>0){cycles--;}
return;
}

unsigned char Read_SPI2_ADC(void)
{ 
	//Write_SPI2_ADC(0);
	//SPI2_Delay(1000);
	return SPI2BUF; /* return byte read */                       
}

void Write_SPI2_ADC(unsigned char data_out)
{  
    SPI2BUF = data_out;	  /*  byte write  */
	while(SPI2STATbits.SPITBF) {} 
}

/****************************************************************************
*     Function Name :  getsSPI2                                             *
*     Description   :  This routine reads a string from the SPI             *
*                      receive buffer.The number of byte/word to be read    *
*                      is determined by parameter 'length'                  *
*     Parameters    :  unsigned int length: The length of data expected     *
*                      unsigned int *rdptr   the received data to be        *
*                      recorded to this array                               *
*                      unsigned int uart_data_wait timeout value            *
*     Return Value  :  unsigned int number of data bytes yet to be received *                     
****************************************************************************/

unsigned int gets_SPI2_ADC(unsigned int length, unsigned int *rdptr, 
                      unsigned int spi_data_wait)
{
    int wait = 0;
    char *temp_ptr = (char *)rdptr;
    while (length)                         /* stay in loop until length = 0  */
    {
        while(!SPI2STATbits.SPIRBF)
        {

           if(wait < spi_data_wait)
              wait++ ;                     /* wait for time out operation */
           else
              return(length);              /* Time out, return number of byte/word read 
                                              for a given length */
        }
        wait = 0;
        if(SPI2CON1bits.MODE16) *rdptr++ = Read_SPI2_ADC();
        else *temp_ptr++ = Read_SPI2_ADC();
        length--;                          /* reduce string length count by 1*/
    }

    return (length);                       /* return number of byte/word to be read i.e, 0 */
}


/******************************************************************************
*     Function Name :  putsSPI2                                               *
*     Description   :  This function writes the specified length of data      *
*                      word/byte to be transmitted into the transmit buffer   *
*     Parameters    :  unsigned int: length of bytes/words to be written      *
*                      unsigned int*:  Address ofthe location where the data  *
*                      to be transmitted is stored                            *
*     Return Value  :  None                                                   *                                                                
******************************************************************************/

void puts_SPI2_ADC(unsigned int length, unsigned int *wrptr)
{  
    char *temp_ptr = (char *) wrptr;
    while (length)                   /* write byte/word until length is 0 */
    {           
        if(SPI2CON1bits.MODE16)
            SPI2BUF = *wrptr++;      /* initiate SPI bus cycle by word write */ 
        else
            SPI2BUF = *temp_ptr++;   /* initiate SPI bus cycle by byte write */ 
        while(SPI2STATbits.SPITBF);  /* wait until 'SPITBF' bit is cleared */
        length--;                    /* decrement length */
    }
}


